import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Hashtable;


public class Relations {

	public JFrame frame;
	public static JTextField par1;
	public static JTextField par2;
	public static JTextField spouse;
	public static JTextField aPar1;
	public static JTextField aPar2;

	String id;
	public static int button;
	public static int buttonToWatch;
	
	public static Hashtable<Integer,Person> people;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Relations window = new Relations();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Relations() {
		initialize();
		par1.setText("");
		par2.setText("");
		spouse.setText("");
		aPar1.setText("");
		aPar2.setText("");
		people = new Hashtable<Integer,Person>();
		id = MainScreen.personId;
		frame.setTitle(MainScreen.personSelected);
		populate();
	}
	
	public void populate(){
		Database db = new Database();
		ArrayList<Person> par = db.getParents(id);
		if(par != null){
			if(par.size() == 1){
				people.put(1, par.get(0));
				par1.setText(par.get(0).fname + " " + par.get(0).lname);
			}else if(par.size() == 2){
				people.put(1, par.get(0));
				par1.setText(par.get(0).fname + " " + par.get(0).lname);
				people.put(2, par.get(1));
				par2.setText(par.get(1).fname + " " + par.get(1).lname);
			}
		}
		ArrayList<Person> mar = db.getMarriedTo(id);
		if(mar != null){
			if(mar.size() > 0){
				people.put(3, mar.get(0));
				spouse.setText(mar.get(0).fname + " " + mar.get(0).lname);
			}
		}
		ArrayList<Person> ad = db.getAdoptedBy(id);
		if(ad != null){
			if(ad.size() == 1){
				people.put(4, ad.get(0));
				aPar1.setText(ad.get(0).fname + " " + ad.get(0).lname);
			}else if(ad.size() == 2){
				people.put(4, ad.get(0));
				aPar1.setText(ad.get(0).fname + " " + ad.get(0).lname);
				people.put(5, ad.get(1));
				aPar2.setText(ad.get(1).fname + " " + ad.get(1).lname);
			}
		}
	}
	
	public void toDatabase(){
		Database db = new Database();
		boolean result;
		String desc = "";
		int count = 0;
		System.out.println("1");
		if(people.containsKey(2)){
			result = db.updateParents(id, people.get(1).id, people.get(2).id);
			desc += "Changed Parents";
			count++;
		}else if(people.containsKey(1)){
			result = db.updateParents(id, people.get(1).id);
			desc += "Changed Parents";
			count++;
		}
		System.out.println("2");
		if(people.containsKey(3)){
			result = db.updateMarriedTo(id, people.get(3).id);
			if(count == 0){
				desc += "Changed Marriage";
			}else{
				desc += ", Marraige";
			}
			count++;
		}
		System.out.println("3");
		if(people.containsKey(5)){
			result = db.updateAdoptedBy(id, people.get(4).id, people.get(5).id);
			if(count == 0){
				desc += "Changed Adoption";
			}else{
				desc += ", Adpotion";
			}
		}else if(people.containsKey(4)){
			result = db.updateAdoptedBy(id, people.get(4).id);
			if(count == 0){
				desc += "Changed Adoption";
			}else{
				desc += ", Adoption";
			}
		}
		System.out.println("4");
		if(desc.equals("")){
			desc = "Nothing";
		}
		db.updateEdits(MainScreen.theUser, desc, id);
		frame.setVisible(false);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 430);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblParents = new JLabel("Parents:");
		lblParents.setBounds(22, 24, 64, 14);
		frame.getContentPane().add(lblParents);
		
		par1 = new JTextField();
		par1.setEnabled(false);
		par1.setBounds(22, 49, 121, 20);
		frame.getContentPane().add(par1);
		par1.setColumns(10);
		
		par2 = new JTextField();
		par2.setEnabled(false);
		par2.setBounds(153, 49, 121, 20);
		frame.getContentPane().add(par2);
		par2.setColumns(10);
		
		JLabel lblSpouse = new JLabel("Spouse:");
		lblSpouse.setBounds(22, 111, 64, 14);
		frame.getContentPane().add(lblSpouse);
		
		spouse = new JTextField();
		spouse.setEnabled(false);
		spouse.setBounds(22, 136, 163, 20);
		frame.getContentPane().add(spouse);
		spouse.setColumns(10);
		
		JButton selPar1 = new JButton("Select");
		selPar1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				button = 1;
				buttonToWatch = 2;
				ChildOf c = new ChildOf();
				c.frmChoice.setVisible(true);
			}
		});
		selPar1.setBounds(42, 80, 85, 23);
		frame.getContentPane().add(selPar1);
		
		JButton selPar2 = new JButton("Select");
		selPar2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				button = 2;
				buttonToWatch = 1;
				ChildOf c = new ChildOf();
				c.frmChoice.setVisible(true);
			}
		});
		selPar2.setBounds(163, 80, 89, 23);
		frame.getContentPane().add(selPar2);
		
		JButton selSouse = new JButton("Select");
		selSouse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				button = 3;
				ChildOf c = new ChildOf();
				c.frmChoice.setVisible(true);
			}
		});
		selSouse.setBounds(54, 167, 89, 23);
		frame.getContentPane().add(selSouse);
		
		JLabel lblDivorcee = new JLabel("Divorcee: ");
		lblDivorcee.setBounds(22, 201, 76, 14);
		frame.getContentPane().add(lblDivorcee);
		
		JButton addDiv = new JButton("Add Divorcee");
		addDiv.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				button = 6;
				ChildOf c = new ChildOf();
				c.frmChoice.setVisible(true);
			}
		});
		addDiv.setBounds(22, 222, 121, 23);
		frame.getContentPane().add(addDiv);
		
		JLabel lblAdoptiveParents = new JLabel("Adoptive Parents:");
		lblAdoptiveParents.setBounds(22, 256, 121, 14);
		frame.getContentPane().add(lblAdoptiveParents);
		
		aPar1 = new JTextField();
		aPar1.setEnabled(false);
		aPar1.setBounds(22, 281, 121, 20);
		frame.getContentPane().add(aPar1);
		aPar1.setColumns(10);
		
		aPar2 = new JTextField();
		aPar2.setEnabled(false);
		aPar2.setBounds(153, 281, 121, 20);
		frame.getContentPane().add(aPar2);
		aPar2.setColumns(10);
		
		JButton submit = new JButton("Submit");
		submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				toDatabase();
			}
		});
		submit.setBounds(335, 357, 89, 23);
		frame.getContentPane().add(submit);
		
		JButton cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
			}
		});
		cancel.setBounds(236, 357, 89, 23);
		frame.getContentPane().add(cancel);
		
		JButton selAPar1 = new JButton("Select");
		selAPar1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				button = 4;
				buttonToWatch = 5;
				ChildOf c = new ChildOf();
				c.frmChoice.setVisible(true);
			}
		});
		selAPar1.setBounds(42, 312, 89, 23);
		frame.getContentPane().add(selAPar1);
		
		JButton selAPar2 = new JButton("Select");
		selAPar2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				button = 5;
				buttonToWatch = 4;
				ChildOf c = new ChildOf();
				c.frmChoice.setVisible(true);
			}
		});
		selAPar2.setBounds(163, 312, 89, 23);
		frame.getContentPane().add(selAPar2);
	}
}
